package com.hmsm.sys.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hmsm.sys.po.DeptInfo;
import com.hmsm.sys.po.UserInfo;
import com.hmsm.sys.service.DeptInfoService;
import com.hmsm.sys.service.UserInfoService;

@Controller("userInfoController")
@Scope("prototype")
@RequestMapping("/")
public class UserInfoController {
	
	private static final Log log = LogFactory.getLog(UserInfoController.class);
	
	@Resource
	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	
	@Resource
	private DeptInfoService deptInfoService;
	
	public DeptInfoService getDeptInfoService() {
		return deptInfoService;
	}

	public ModelAndView edit(int pkId, Integer deptId, UserInfo currentUser, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("success");

		DeptInfo deptInfo = null;
		UserInfo user = null;
		try {
			if(deptId!=null){
				deptInfo = getDeptInfoService().getById(deptId);
			}
			user = getUserInfoService().getForModify(pkId, deptId, currentUser);
			List<DeptInfo> managedOrgList = getDeptInfoService().getManagedOrgSelectList(currentUser);
			modelAndView.getModel().put("managedOrgList", managedOrgList);
			List<DeptInfo> departSelectList = getDeptInfoService().getDeptSelectList(currentUser.getOrg()==null?null:currentUser.getOrg().getPkId());
			modelAndView.getModel().put("departSelectList", departSelectList);
			if (user==null){
				String errorMessage = "要编辑的用户不存在！";
				modelAndView.getModel().put("errorMessage", errorMessage);
				modelAndView.setViewName("error");
			}
			modelAndView.getModel().put("deptInfo", deptInfo);
			modelAndView.getModel().put("user", user);
		} catch (Exception e) {
			String errorMessage = "发生错误+"+e;
			modelAndView.getModel().put("errorMessage", errorMessage);
			modelAndView.setViewName("error");
		}
		
		return modelAndView;
	}

}
